package com.sccc.Filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;  

public class PageFilter extends HttpServlet implements Filter {

	public void destroy() {}  

	 public void doFilter(ServletRequest sRequest, ServletResponse sResponse,        
             FilterChain filterChain) throws IOException, ServletException{  
           
         HttpServletRequest request = (HttpServletRequest) sRequest;        
         HttpServletResponse response = (HttpServletResponse) sResponse;        
         HttpSession session = request.getSession();        
         String url=request.getServletPath();    
         String contextPath=request.getContextPath();    
         if(url.equals("")) url+="/";  


         //放行需要访问的目录
         if((url.startsWith("/")
        		 &&!url.startsWith("/login")&&!url.startsWith("/index.jsp")
        		 &&!url.startsWith("/View/images")&&!url.startsWith("/View/images")
                 &&!url.startsWith("/View/js")
        		 &&!url.startsWith("/View/css")
        		 &&!url.startsWith("/View/bootstrap")
        		 &&!url.startsWith("/View/admin/alluser.jsp")
        		 &&!url.startsWith("/View/admin/server.jsp"))){//若访问后台资源 过滤到login
        	 
              String user=(String)session.getAttribute("userName");  
              String admin=(String)session.getAttribute("adminName");  
              if(user==null || admin==null){//转入管理员登陆页面    
                   response.sendRedirect(contextPath+"/goLoginAndRegAction");   
                   return;    
              }   
              
         }    
           filterChain.doFilter(sRequest, sResponse);      
     }   
	 
	 public void init(FilterConfig arg0) throws ServletException {  
		   
     } 

}
